from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
import time
class Base():
    def __init__(self,driver):
        self.driver=driver
    def open_url(self,url):
        self.driver.get(url)
    def error_png(self):
        error_png=time.strftime("%Y%m%d-%H%M%S")
        return error_png
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)

        except Exception as e:
            self.driver.execute_script("../Png/"+self.error_png()+".png")
            print(e)
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    def click(self,loc):
        self.find_element(loc).click()
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    def scrool_x(self,x):
        self.driver.execute_script(f"window.scrollTo(0,{x})")
    def select_loc_value(self,loc,info):
        value=Select(self.find_element(loc))
        value.select_by_value(info)
    def select_loc_index(self,loc,num):
        index=Select(self.find_element(loc))
        index.select_by_index(num)
    def select_loc_text(self,loc,info):
        text=Select(self.find_element(loc))
        text.select_by_visible_text(info)